#!/usr/bin/python
# -*- coding: UTF-8 -*-
from bs4 import BeautifulSoup
from selenium import webdriver;
from selenium.common.exceptions import TimeoutException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import json
import requests
from hashlib import md5
import os
import re
browser=webdriver.Chrome()
# browser.set_window_size(1400,900)
wait=WebDriverWait(browser, 10);


def search():
    url="https://xueqiu.com/u/7340804633"
    browser.get(url)
    content = wait.until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "#app > div.container.profiles__main__container > div.profiles__main"))

    )
    get_article()


def get_article():
    html=browser.page_source;
    ptt=re.compile('<img.*?src="(.*?)"')
    results=ptt.findall(html)
    urls=[]
    for result in results:
        if result.startswith("https"):
            download_image(result)




def download_image(url):
    print('Downloading', url)
    try:
        response = requests.get(url)
        if response.status_code == 200:
            save_image(response.content)
        return None
    except ConnectionError:
        return None


def save_image(content):
    folder = os.getcwd() + "\\img"
    if not os.path.exists(folder):
        os.mkdir(folder)
    file_path = '{0}/{1}.{2}'.format(folder, md5(content).hexdigest(), 'jpg')
    print(file_path)
    if not os.path.exists(file_path):
        with open(file_path, 'wb') as f:
            f.write(content)
            f.close()


def next_page(i):
    click = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.pagination .pagination__next')));
    click.click()
    content = wait.until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "#app > div.container.profiles__main__container > div.profiles__main"))

    )
    get_article()



def main():
    search()
    for i in range(1,3):
        next_page(i)


if __name__ == '__main__':
    main()